package com.work.cloudseata;

import com.work.cloudseata.entity.OperationLog;
import com.work.cloudseata.service.OperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.Message;

import java.util.function.Consumer;

@Slf4j
@SpringBootApplication
public class LoggingServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(LoggingServiceApplication.class, args);
    }

    @Bean
    public Consumer<Message<String>> log(OperationLogService service) {
        return message -> {
            String ip = message.getHeaders().get("ip", String.class);
            Long userId = Long.valueOf(message.getHeaders().get("userId", String.class));
            OperationLog entity = new OperationLog();
            entity.setUserId(userId);
            entity.setIp(ip);
            entity.setAction("register");
            entity.setDetail(message.getPayload());
            service.save(entity);
        };
    }
}
